/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.leeloo.databinding.internal.ui;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.jface.viewers.TreeNode;
import org.leeloo.core.Tag;
import org.leeloo.internal.core.ObjectDescriptor;

/**
 * Builds the children of a node.
 * 
 * @author Brad Reynolds
 */
public class LabeledTreeNodeBuilder {
    private LabeledTreeNode parentNode;

    private List children;

    private LabeledTreeNodeBuilder(LabeledTreeNode node) {
        this.parentNode = node;
    }

    /**
     * Creates a builder for an existing node.
     * 
     * @param parent
     * @return builder
     */
    public static LabeledTreeNodeBuilder forNode(LabeledTreeNode parent) {
        return new LabeledTreeNodeBuilder(parent);
    }

    /**
     * Adds a child as a {@link LabeledTreeNode} even if the child is
     * <code>null</code>.
     * 
     * @param value
     *            can be <code>null</code>
     * @param label
     */
    public void addChild(Object value, String label) {
        addChild(value, label, true);
    }

    /**
     * Adds a child as a {@link LabeledTreeNode}.
     * 
     * @param value
     *            can be <code>null</code>, see addIfNull
     * @param label
     * @param addNull
     *            <code>true</code> if wanting to add a node if value is null
     */
    public void addChild(Object value, String label, boolean addNull) {
        if (value == null && !addNull) {
            return;
        }

        LabeledTreeNode child = null;
        if (value instanceof ObjectDescriptor) {
            child = (LabeledTreeNode) ((ObjectDescriptor) value).getAdapter(LabeledTreeNode.class);
            assert child != null;
        } else {
            child = new LabeledTreeNode(value);
        }
        addToChildren(child, label);
    }

    /**
     * @return build node
     */
    public LabeledTreeNode build() {
        List nodes = new LinkedList();
        if (parentNode.getChildren() != null) {
            nodes.addAll(Arrays.asList(parentNode.getChildren()));
        }

        if (children != null) {
            nodes.addAll(children);
        }

        parentNode.setChildren((TreeNode[]) nodes.toArray(new LabeledTreeNode[nodes.size()]));
        
        return parentNode;
    }
    
    /**
     * Adds a child that is a {@link Tag}.
     * 
     * @param tag
     * @param label
     * @param editable 
     */
    public void addTagChild(Tag tag, String label, boolean editable) {
        if (tag == null) {
            throw new IllegalArgumentException("Parameter tag was null."); //$NON-NLS-1$
        }
        
        addToChildren(new LabeledTagTreeNode(parentNode.getValue(), tag, editable), label);
    }
    
    /**
     * Adds the node to the children list.
     * 
     * @param child
     * @param label
     */
    private void addToChildren(LabeledTreeNode child, String label) {
        if (children == null) {
            children = new LinkedList();
        }
        
        child.setLabel(label);
        child.setParent(parentNode);
        children.add(child);
    }
}
